package com.leave.dao;

import com.leave.entity.Classes;
import com.leave.tools.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassesDao {
    private JdbcTemplate jdbcTemplate;

    public List<Classes> query(Classes classes){

        String sql = "select * from sys_classes where 1 = 1 ";
        List<Object> paramValues = new ArrayList<>();
        List<Classes> classesList = new ArrayList<>();

        if(classes!=null){
            if(classes.getClassID() != null&&!"".equals(StringUtils.trim(classes.getClassID()))){
                sql += "and classID=?";
                paramValues.add(classes.getClassID());
            }
        }

        classesList = jdbcTemplate.query(sql, paramValues.toArray(), new RowMapper<Classes>() {
            @Override
            public Classes mapRow(ResultSet rs, int rowNum) throws SQLException {
                Classes classes = new Classes();
                String classID = rs.getString("classID");
                String className =rs.getString("className");
                String depID =rs.getString("depID");
                String major =rs.getString("major");
                String grade =rs.getString("grade");
                classes.setClassID(classID);
                classes.setClassName(className);
                classes.setDepID(depID);
                classes.setMajor(major);
                classes.setGrade(grade);
                return classes;
            }
        });
        return classesList;
    }

    public void delete(String classID){
        String sql = "delete from sys_classes where classID=?";
        String[]paramValues = {classID};
        jdbcTemplate.update(sql,paramValues);
    }

    public void insert(Classes classes) {
        //声明插入SQL字符串变量sql
        String sql = "insert into sys_classes(classID,className,depID,major,grade)values(?,?,?,?,?)";
        if (classes != null) {
            String[] paramValues = {classes.getClassID(), classes.getClassName(),classes.getDepID(), classes.getMajor(), classes.getGrade()};
            jdbcTemplate.update(sql, paramValues);
        }
    }

    public List<Map<String, Object>> selectClassesList(String keyword) throws SQLException {
        String sql = "SELECT * FROM sys_classes WHERE classID LIKE ? OR className LIKE ? OR depID LIKE ? OR major LIKE ? OR grade LIKE ? ";
        String likeKeyword = "%" + keyword + "%";
        String[] paramValues = {likeKeyword, likeKeyword, likeKeyword, likeKeyword, likeKeyword};
        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String, Object>>() {
            @Override
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                Map<String, Object> map = new HashMap<>();
                int columnCount = rs.getMetaData().getColumnCount();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = rs.getMetaData().getColumnName(i);
                    Object columnValue = rs.getObject(i);
                    map.put(columnName, columnValue);
                }
                return map;
            }
        };
        return jdbcTemplate.query(sql, paramValues, rowMapper);
    }

    public void update(Classes classes) {
        // 声明更新 SQL 字符串变量 sql
        String sql = "update sys_classes set classID=?, className=?, depID=?, major=?, grade=? where classID=?";
        if (classes != null) {
            String[] paramValues = {classes.getClassID(), classes.getClassName(), classes.getDepID(), classes.getMajor(), classes.getGrade(), classes.getClassID()};
            jdbcTemplate.update(sql, paramValues);
        }
    }
    /**
     *  setter注入方式，注意不需要定义getter方法。
     * @param jdbcTemplate
     */
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
